package cyou.breathe.zsxq;

import com.vladsch.flexmark.html2md.converter.FlexmarkHtmlConverter;
import cyou.breathe.constant.AsciiBanner;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;
import java.util.regex.Pattern;

/**
 * @author: breathe
 * @createTime: 2025-07-28
 */
public class Main {


    private static final String cookie = "tfstk=gZ6x3tgs1z4DQUJ3n-NoItWZb6Eo5OI4wZSIIFYm1aQRAiOsjEDMC_Q1zIYb7s0tBGsInEOXcYMWxngsCItXF3tHjNda0FY6WNj1iu23KiS2QdTT-J2H9wpgRZhjCAZ6FHtUqnNLCixJQda3r9amhIvN83iDwOs72hxZcA_6lgZJmHpshCMjN4Tyfd96ljgWNnxXfAT6cuIWzhp65N9sB1Fvymt-B661Dq4iPcMELTKvMFHMDAClhvA6ROYrCm6JDVTCcUHsBE2f5HSRmrHFqKWhJHb0FxQdfM56wO37JFWRVt114qU641_DFEvQG0pc3i1B19aZ1wpAWB6XwcMC0iTye36QjX8leEb1HQZnpCv5KB9fZ7zDsKKd5t7xXA_CqMX2aOUKPF5DYLtOQkG9WCsz99XKx5uHJhcb2uhZ_ItPIZOlcml4fwtJ-oKx_fR643L32uGZ_Itk2eqxYflwaAC..; zsxq_access_token=E7BA3642-8BF0-4421-B65C-AEEB47AFE58F_FCEC0247B3AAB07E; abtest_env=product; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%22184418145558882%22%2C%22first_id%22%3A%221926c241277482-0729ede13ce466-26001051-2359296-1926c241278566%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%2C%22%24latest_referrer%22%3A%22%22%7D%2C%22identities%22%3A%22eyIkaWRlbnRpdHlfY29va2llX2lkIjoiMTkyNmMyNDEyNzc0ODItMDcyOWVkZTEzY2U0NjYtMjYwMDEwNTEtMjM1OTI5Ni0xOTI2YzI0MTI3ODU2NiIsIiRpZGVudGl0eV9sb2dpbl9pZCI6IjE4NDQxODE0NTU1ODg4MiJ9%22%2C%22history_login_id%22%3A%7B%22name%22%3A%22%24identity_login_id%22%2C%22value%22%3A%22184418145558882%22%7D%2C%22%24device_id%22%3A%221926c241277482-0729ede13ce466-26001051-2359296-1926c241278566%22%7D";
    private static final List<String> urls = List.of(
    );
    private static final String output = "D:/note/zsxq/oneThread";
    public static void main(String[] args) throws IOException {
        System.out.println(AsciiBanner.get());
        urls.forEach(Main::download);
//        String removeString = "content";
//        removeContent("", removeString, "");
    }

    public static void download(String url) {


        //通过jsoup和上面的url爬取网站html，保存到本地
        try {
            Connection connect = Jsoup.connect(url);
            connect.cookie("COOKIE", cookie);
            Document elements = connect.get();
            String html = elements.html();
            System.out.println(elements.title());
            String md = FlexmarkHtmlConverter.builder().build().convert(html);
            //将这个html写入output的路径，创建一个output文件夹
            File file = new File(output);
            if (!file.exists()) {
                file.mkdirs();
            }
            FileWriter fileWriter = new FileWriter(output + "/" + elements.title() + ".md");
            fileWriter.write(md);
            fileWriter.close();
            System.out.println("写入完成");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void replaceContent(String regex, String removeString, String replacement) {
        File file = new File(output);
        for (File mdFile : file.listFiles()) {
            if (mdFile.isFile() && mdFile.getName().endsWith(".md")) {
                try {
                    //读取mdFile文件中的内容
                    String content = Files.readString(mdFile.toPath());
                    if (replacement == null || replacement.length() == 0) {
                        replacement = "";
                    }
                    if (regex == null || regex.length() == 0) {
                        regex = Pattern.quote(removeString);
                    }
                    // 编译正则表达式，使用DOTALL让.匹配包括换行符在内的所有字符
                    Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);

                    // 替换匹配的内容
                    String modifiedContent = pattern.matcher(content).replaceAll(replacement);

                    Files.writeString(mdFile.toPath(), modifiedContent);
                    System.out.println("成功移除正则匹配内容，结果已保存到 " + mdFile.toPath());
                } catch (Exception e) {
                    System.err.println("处理文件时发生错误: " + e.getMessage());
                    e.printStackTrace();
                }
            }
        }
    }
}
